package com.juitar.base.fingering;

import java.util.Iterator;
import java.util.List;

import com.juitar.base.Chord;
import com.juitar.base.Fingering;
import com.juitar.base.SimpleFingering;
import com.juitar.base.Tuning;

/**
 * @author SKopylov
 * 
 * Presents fingerings as text like this
 * <pre>
 * 
 * O---|---|---|---|
 * |-O-|---|---|---|
 * O---|---|---|---|
 * |---|-O-|---|---|
 * |---|---|-O-|---|
 * X---|---|---|---|
 *
 *</pre>
 *	
 */
public class TextFingering {

	private static String[] strings =
	{
		"X---|---|---|---|",
		"O---|---|---|---|",
		"|-O-|---|---|---|",
		"|---|-O-|---|---|",
		"|---|---|-O-|---|",
		"|---|---|---|-O-|",
		};
	
	private static String[] fretNum =
		{
			"  1              ",
			"  2              ",
			"  3              ",
			"  4              ",
			"  5              ",
			"  6              ",
			"  7              ",
			"  8              ",
			"  9              ",
			"  10             ",
			"  11             ",
			"  12             ", 
			"  13             ",
			"  14             ",
			"  15             ",
			"  16             "};

	public static String textFingerings(Chord chord, Tuning tuning) {
		String ret = "";
		List<Fingering> list =
			FingeringBuilder.create(chord, tuning, null);
		for (Iterator<Fingering> iter = list.iterator(); iter.hasNext();) {
			SimpleFingering f = iter.next();
			String[] s = TextFingering.toStrings(f);
			for (int i = 0; i < s.length; i++) {
				ret += s[i] + "\n";
			}
			ret += "\n";
		}
		return ret;
	}

	public static String[] toStrings(SimpleFingering fing) {
		String[] ret = new String[fing.strings() + 1];
		int min = fing.getFirstFret();
		if (min==0) {
			//System.out.println(fing);
			min =1;
		}
		ret[0] = fretNum[min - 1];
		for (int i = 0; i < fing.strings(); i++) {
			int fret = fing.getFret(i);
			if (fret < 0) {
				ret[i + 1] = strings[0];
			} else if (fret == 0) {
				ret[i + 1] = strings[1];
			} else {
				String s = strings[fret - min + 2];
				ret[i + 1] = s;
			}
		}
		return ret;
	}
}
